<template>
  <view>
    <view>
      <u-form :model="queryParams" ref="uQueryForm">
        <u-form-item label="报工" >
          <u--input
              placeholder="请输入报工机编号"
              border="surround"
              :disabled="true"
              v-model="this.deviceCode"
          ></u--input>
        </u-form-item>
        <u-form-item label="工位" >
          <u--input
              placeholder="请输入工位"
              border="surround"
              :disabled="true"
              v-model="this.workStation"
          ></u--input>
        </u-form-item>
        <u--input
            placeholder="请输入工单号"
            border="surround"
            v-model="queryParams.woCode"
            clearable
            @change="queryWorks"
        ></u--input>
        <u-form-item label="工单" v-if="tempShow">
          <uni-data-select v-model="tempWoCode" placeholder="请选择工单" clearable
                           :localdata="woCodes" @change="changeWoCode"/>
        </u-form-item>
        <u-form-item label="扫码" >
          <u--input
              placeholder="扫码"
              border="surround"
              v-model="queryParams.searchKey"
              clearable
              @change="changeCode"
          ></u--input>
        </u-form-item>
      </u-form>
    </view>
    <view >
      <u-modal :show="show" :title="title" showCancelButton confirmText="提交" @cancel="cancelForm"  @confirm="submitForm">
        <u-form :model="form" ref="uForm">
          <u-form-item label="工单">
            <u--input
                placeholder="请输入工单号"
                border="surround"
                clearable
                v-model="form.woCode"
            ></u--input>
          </u-form-item>
          <u-form-item label="物料编码">
            <u--input
                placeholder="请输入物料编码"
                border="surround"
                clearable
                v-model="form.invCode"
            ></u--input>
          </u-form-item>
          <u-form-item label="批次号">
            <u--input
                placeholder="请输入批次号"
                border="surround"
                clearable
                v-model="form.batchNo"
            ></u--input>
          </u-form-item>
          <u-form-item label="需求数量">
            <u--input
                placeholder="请输入需求数量"
                border="surround"
                v-model="form.needQty"
            ></u--input>
          </u-form-item>
          <u-form-item label="报工数量">
            <u--input
                placeholder="请输入报工数量"
                border="surround"
                clearable
                v-model="form.qty"
            ></u--input>
          </u-form-item>
          <u-form-item label="站位表">
            <uni-data-select v-model="guidePosition" placeholder="请选择站位表" clearable
                             :localdata="form.guidePosition" @change="changeGuidePosition"/>
          </u-form-item>
          <u-form-item label="位置">
            <u--input
                placeholder="请输入位置"
                border="surround"
                clearable
                v-model="form.position"
            ></u--input>
          </u-form-item>
        </u-form>
      </u-modal>
    </view>
    <view class="fixed-bottom">
      <u-button @click="scanCode">扫码上料</u-button>
    </view>
  </view>
</template>


<script>

import UButton from "../../../../uview-ui/components/u-button/u-button.vue";
import UFormItem from "../../../../uview-ui/components/u-form-item/u-form-item.vue";
import UniDataSelect from "../../../../uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue";
import USwitch from "../../../../uview-ui/components/u-switch/u-switch.vue";
import UInput from "../../../../uview-ui/components/u--input/u--input.vue";
import UForm from "../../../../uview-ui/components/u--form/u--form.vue";
import {getWorks} from "../../../../api/work/productLine/load";
import UModal from "../../../../uview-ui/components/u-modal/u-modal.vue";
import {addLoadData, getLoadDatas} from "../../../../api/work/productLine/scanLoad";

export default {
  data() {
    return {
      queryParams: {
        woCode: null,
        indivisible: false,
        pageNum: 1,
        pageSize: 20,
        searchKey: null,
      },
      tempShow: false,
      chooseFlag: false,
      tempWoCode: null,
      guidePosition: null,
      woCodes: [{value: '测试', text: '测试'}],
      show: false,
      title: "上料信息",
      deviceCode: null,
      workStation: null,
      form: {
        deviceCode: this.deviceCode,
        workStation: this.workStation,
        woCode: null,
        invCode: null,
        batchNo: null,
        needQty: null,
        qty: null,
        guidePosition: [{value: '测试', text: '测试'}],
        position: null,
      }
    }
  },
  onLoad() {
    this.deviceCode  = uni.getStorageSync('reportCode')
    this.workStation  = uni.getStorageSync('workStation')
  },
  methods: {
    submitForm() {
      console.log(this.form)
      addLoadData(this.form).then(() => {
        this.show = false;
        this.$modal.msgSuccess("提交成功");
      });
    },
    cancelForm() {
      this.show = false;
    },
    changeCode(result) {
      if (!this.chooseFlag) {
        this.$modal.showToast('请选择工单')
        return;
      }
      if (this.deviceCode === null || this.deviceCode === '' || this.deviceCode.trim() === '') {
        this.$modal.showToast('报工机编号不能为空')
        return;
      }
      if (this.workStation === null || this.workStation === '' || this.workStation.trim() === '') {
        this.$modal.showToast('工位不能为空')
        return;
      }
      let deviceCode = uni.getStorageSync('reportCode');
      if (deviceCode === null) {
        this.$modal.showToast('请设置报工机编号')
        return;
      }
      if (result === null || result === '') {
        return;
      }
      let datas = result.split("#");
      let formType = 0;
      if (datas.length === 7 && result.toUpperCase().includes("ALL")) {
        formType = 1;
      } else if (datas.length !== 8 && result.toUpperCase().includes("ALL")) {
        formType = 2;
      } else {
        this.$modal.showToast('格式不对:扫码内容为：' + result)
        return;
      }
      this.showUpData(formType, datas);
    },
    scanCode() {
      if (!this.chooseFlag) {
        this.$modal.showToast('请选择工单')
        return;
      }
      if (this.deviceCode === null || this.deviceCode === '' || this.deviceCode.trim() === '') {
        this.$modal.showToast('报工机编号不能为空')
        return;
      }
      if (this.workStation === null || this.workStation === '' || this.workStation.trim() === '') {
        this.$modal.showToast('工位不能为空')
        return;
      }
      let deviceCode = uni.getStorageSync('reportCode');
      if (deviceCode === null) {
        this.$modal.showToast('请设置报工机编号')
        return;
      }
      uni.scanCode({
        success: (res) => {
          let datas = res.result.split("#");
          let formType = 0;
          if (datas.length === 7 && res.result.toUpperCase().includes("ALL")) {
            formType = 1;
          } else if (datas.length !== 8 && res.result.toUpperCase().includes("ALL")) {
            formType = 2;
          } else {
            this.$modal.showToast('格式不对:扫码内容为：' + res.result)
            return;
          }
         this.showUpData(formType, datas);
        },
        fail: (err) => {
          this.$modal.showToast('扫码失败', err)
        }
      });
    },
    showUpData(formType, datas) {
      this.resetForm();
      this.form.woCode = this.tempWoCode;
      if (formType === 1) {
        this.form.invCode = datas[0];
        this.form.batchNo = datas[1];
      } else {
        this.form.invCode = datas[1];
        this.form.batchNo = datas[2];
      }
      let param = {
        woCode: this.form.woCode,
        deviceCode: this.deviceCode,
        invCode: this.form.invCode,
      }
      getLoadDatas(param).then(res => {
        let datas = res.data;
        if (datas.length === 0) {
          this.$modal.showToast('该物料不需要上料')
          return;
        }
        this.form.needQty = datas[0].qty;
        this.form.qty = datas[0].qty;
        this.form.guidePosition = [];
        datas.forEach(data => {
          if (data.position !== null && data.position !== '') {
            data.position.split('|').forEach(item => {
              if (item !== null && item !== '') {
                let ele =  {
                  value: item,
                  text: item,
                }
                this.form.guidePosition.push(ele);
              }
            });
          }
        });
        if (this.form.guidePosition.length !== 0) {
          this.guidePosition = this.form.guidePosition[0].value;
          this.form.position = this.guidePosition;
        }
        this.show = true;
      });
    },
    resetForm() {
      this.form = {
            deviceCode: this.deviceCode,
            workStation: this.workStation,
            woCode: null,
            invCode: null,
            batchNo: null,
            needQty: null,
            qty: null,
            guidePosition: null,
            position: null,
      }
    },
    queryWorks(query) {
      this.chooseFlag = false;
      this.woCodes = [];
      getWorks(query).then(response => {
        this.woCodes = response.data.map((data) => {
          return {
            value: data,
            text: data,
          }
        });
        this.tempShow = true;
      });
    },
    changeWoCode() {
      this.tempShow = false;
      this.queryParams.woCode = this.tempWoCode;
      this.chooseFlag = true;
    },
    changeGuidePosition() {
      this.form.position = this.guidePosition;
    },
  },
  components: {
    UModal,
    UForm, UInput, USwitch, UniDataSelect, UFormItem,
    UButton

  }
};
</script>
<style>
.fixed-bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  /* 可能需要设置一些内边距或外边距以保持良好的视觉效果 */
  padding: 10px;
  text-align: center;
  background-color: #fff; /* 背景色与页面背景色相同或透明，视设计而定 */
  z-index: 999; /* 确保按钮在最上层 */
}
</style>
